package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.JianCeList;
import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.CircularImage;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 检测报告
 */
public class JianCeReportListActivity extends BaseActivity {

    @BindView(R.id.rlist_jc)
    RecyclerView rlistJc;
    @BindView(R.id.refresh_jc)
    SwipeRefreshLayout mRefresh;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<JianCeList.RowsBean> Temp_list = new ArrayList<>();
    ArrayList<JianCeList.FooterBean> Temp_listfw = new ArrayList<>();//后面追加服务列表
    private JianceListAdapter mAdapter;
    private int page = 1;
    private LinearLayout li_ceshi_null;
    private JianCeList model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jian_ce_report);
        ButterKnife.bind(this);
        ChangLayTitle("检测报告");
        LayBack();
        init();
        getData();
    }

    public void init() {
        //空
        li_ceshi_null = (LinearLayout) findViewById(R.id.li_ceshi_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistJc.setLayoutManager(linearLayoutManager);
        mAdapter = new JianceListAdapter(JianCeReportListActivity.this, rlistJc, Temp_list, R.layout.item_jiance);
        rlistJc.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (Temp_list.get(position + 1).getIsService() == 0) {
                    Intent intent = new Intent(JianCeReportListActivity.this, JianCeXQActivity.class);
                    intent.putExtra("aid", Temp_list.get(position + 1).getAssessId());
                    intent.putExtra("userurl", Temp_list.get(position + 1).getUserhead());
                    intent.putExtra("uname", Temp_list.get(position + 1).getUserName());
                    intent.putExtra("sg", Temp_list.get(position + 1).getStature());
                    intent.putExtra("weight", Temp_list.get(position + 1).getWeight());
                    intent.putExtra("age", Temp_list.get(position + 1).getAge());
                    intent.putExtra("sex", Temp_list.get(position + 1).getSex());
                    startActivity(intent);
                } else {//服务
                    Intent intent = new Intent(JianCeReportListActivity.this, ServiceXQActivity.class);
                    intent.putExtra("aid", Temp_list.get(position + 1).getAssessId());
                    startActivity(intent);
                }
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
//                page++;
//                getData();
            }
        });
    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.JCReportsList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(JianCeReportListActivity.this, "uid"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(JianCeReportListActivity.this, 0, mRequest,
                new CustomHttpListener(JianCeReportListActivity.this, true, JianCeList.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        model = ((JianCeList) data);
                        if (page == 1) {
                            Temp_list.clear();
                            Temp_listfw.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        Temp_listfw.addAll(model.getFooter());
                        /*//追加服务列表
                        if (Temp_listfw.size() > 0) {
                            for (int i = 0; i < Temp_listfw.size(); i++) {
                                JianCeList.RowsBean bean = new JianCeList.RowsBean();
                                bean.setCreateDate(Temp_listfw.get(i).getCreateDate());
                                bean.setService(Temp_listfw.get(i).getPingjia());
                                bean.setAssessId(Temp_listfw.get(i).getOtoRecordId());
                                bean.setIsService(1);
                                Temp_list.add(bean);
                            }
                        }*/
                        if (Temp_list.size() == 0) {
                            li_ceshi_null.setVisibility(View.VISIBLE);
                            rlistJc.setVisibility(View.GONE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }


    public class JianceListAdapter extends BaseLoadMoreHeaderAdapter<JianCeList.RowsBean> {


        public JianceListAdapter(Context mContext, RecyclerView recyclerView, List<JianCeList.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, final JianCeList.RowsBean s) {
            CircularImage pic = (CircularImage) holder.itemView.findViewById(R.id.imv_jclist_userpic);
            ImageView imv_jclist_sex = (ImageView) holder.itemView.findViewById(R.id.imv_jclist_sex);
            TextView tv_ispg = (TextView) holder.itemView.findViewById(R.id.tv_ispg);
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_jclist_name);
            TextView sg = (TextView) holder.itemView.findViewById(R.id.tv_jclist_sg);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_jslist_time);
            TextView years = (TextView) holder.itemView.findViewById(R.id.tv_jclist_years);
            TextView weight = (TextView) holder.itemView.findViewById(R.id.tv_jclist_tz);
            LinearLayout li_man = (LinearLayout) holder.itemView.findViewById(R.id.li_man);
            //种类-标题
            LinearLayout li_bg_type = (LinearLayout) holder.itemView.findViewById(R.id.li_bg_type);
            TextView tv_bg_type = (TextView) holder.itemView.findViewById(R.id.tv_bg_type);
            //服务
            TextView tv_service_pg = (TextView) holder.itemView.findViewById(R.id.tv_servicelist_pg);
            //检测
            LinearLayout li_jc = (LinearLayout) holder.itemView.findViewById(R.id.li_jc);
            //新加--精确报告+开始锻炼
            TextView tv_jc_report = (TextView) holder.itemView.findViewById(R.id.tv_jc_report);
            TextView tv_jc_start = (TextView) holder.itemView.findViewById(R.id.tv_jc_start);
            LinearLayout li_jiance = (LinearLayout) holder.itemView.findViewById(R.id.li_jiance);
            time.setText(s.getCreateDate());
            if (s.getIsService() == 0) {//检测报告
                tv_service_pg.setVisibility(View.GONE);
                li_jc.setVisibility(View.VISIBLE);
                tv_ispg.setVisibility(View.GONE);
                ImgDataUtil.loadYuanImage(JianCeReportListActivity.this, HttpIP.Base_IpIMage + s.getUserhead(), pic);
                name.setText(s.getUserName());
                sg.setText(s.getStature() + "cm");
                years.setText(s.getAge() + "");
                weight.setText(s.getWeight() + "kg");
//                if (("1").equals(s.getAssessStatus())) {
//                    tv_ispg.setText("评估中...");
//                    tv_ispg.setTextColor(getResources().getColor(R.color.MainColor));
//                } else if(("2").equals(s.getAssessStatus())){
//                    tv_ispg.setText("未评估");
//                    tv_ispg.setTextColor(getResources().getColor(R.color.SeconedGrayColor));
//                }
                //1是男
                if (("1").equals(s.getSex())) {
                    imv_jclist_sex.setBackgroundResource(R.drawable.gender_boy);
                } else {
                    imv_jclist_sex.setBackgroundResource(R.drawable.gender_girl);
                }
            } else {//服务列表
//                tv_service_pg.setVisibility(View.VISIBLE);
//                li_jc.setVisibility(View.GONE);
//                tv_ispg.setVisibility(View.GONE);
//                tv_service_pg.setText(s.getService());
            }
            li_bg_type.setVisibility(View.GONE);
            li_bg_type.setOnClickListener(null);
            //精确报告
            tv_jc_report.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent in=new Intent(JianCeReportListActivity.this,ReportActivity.class);
                    in.putExtra("id",s.getAssessId());
                    startActivity(in);
                }
            });
            //开始锻炼
            tv_jc_start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent  in = new Intent(JianCeReportListActivity.this, TrainListActivity.class);
                    startActivity(in);
                }
            });
        }
    }
}
